<script>
	export default {
		onLaunch: function() {
			console.log('App Launch')
			let token = uni.getStorageSync("token");
			this.$store.commit("setToken", token);

			let lat = uni.getStorageSync("lat");
			this.$store.commit("setLat", lat);

			let lng = uni.getStorageSync("lng");
			this.$store.commit("setLng", lng);

			let sysInfo = uni.getSystemInfoSync();
			// //console.log(JSON.stringify(sysInfo));
			this.$store.commit("setSysInfo", sysInfo);
			let isLogin = this.$store.state.isLogin;
			if (isLogin == false) {
				uni.reLaunch({
					url: "/pages/login/login",
					success: (res) => {},
					fail: (err) => {
						console.log("error=>" + JSON.stringify(err))
					}
				})
			}
			
			
			// #ifdef APP-PLUS
			const systemInfo = uni.getSystemInfoSync();
			let version_number = systemInfo.appWgtVersion;
			this.$request("worker/app/version").then(res => {
				console.log(res)
				let url = res.data.path
				if (res.data.versionName != version_number) {
					uni.showModal({
						title: '提示',
						content: '检测到新版本前往更新',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								uni.showLoading({
									title: "下载中..."
								})
								uni.downloadFile({
									url: url, // 后端返回的线上文件路径
									success: function(res) {
										if (res.statusCode === 200) {
											// 文件到本地
											uni.saveFile({
												tempFilePath: res
													.tempFilePath, //临时路径
												success: function(data) {
													var savedFilePath =
														data.savedFilePath
													// 在app端执行
													// #ifdef APP-PLUS
													let osname = plus.os
														.name
													// 如果是安卓的话弹出提示
													if (osname ==
														'Android') {
														uni.showToast({
															icon: 'none',
															mask: true,
															title: '下载成功',
															duration: 1000
														})
													}
													// #endif
													uni.hideLoading()
													//ios手机直接打开文件，手动存储文件到手机，Android手机从根目录创建文件夹，保存文件并改名
													setTimeout(() => {
														//打开文档查看
														uni.openDocument({
															filePath: data
																.savedFilePath,
															success: function(
																ress
															) {
																console
																	.log(
																		"成功打开文件"
																	)
															},
															fail() {
																console
																	.log(
																		"打开文件失败"
																	)
															}
														})
													}, 1000)
												}
											})

										}
									},
									fail: function(res) {
										console.log(res.errMsg);
									},
									complete: function(res) {
										console.log(res.statusCode);
									},
									progress: function(res) {
										console.log('下载进度' + res.progress);
									}
								});
							}
						}
					})
				}
			})
			// #endif
		},
		onShow: function() {
			// //console.log('App Show')
		},
		onHide: function() {
			// //console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
	@import "./common/css/common.css";

	page {
		background-color: #F6F7F8;
	}
</style>